Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This pull request enhances the Cursor class by adding method chaining support and iteration capabilities. The changes enable developers to chain operations like cursor.execute().fetchone() and iterate directly over cursor results using for row in cursor.execute(sql).
Key changes include:
- Modified the
executemethod to return the cursor object itself for method chaining - Added
__iter__and__next__methods to make the cursor iterable - Comprehensive test coverage validating chaining operations, iteration, error handling, and compatibility
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mssql_python/cursor.py |
Updated execute method return type and added iterator methods for cursor iteration |
tests/test_004_cursor.py |
Added extensive test suite covering method chaining and iteration scenarios |
Comments suppressed due to low confidence (1)
tests/test_004_cursor.py:632
- The test assumes that rows have attribute access (row.user_id, row.user_name) but the cursor returns list/tuple objects. This will cause an AttributeError. Use index access instead: row[0] for user_id and row[1] for user_name.
""")
bewithgaurav
reviewed
Aug 4, 2025
Collaborator
bewithgaurav
left a comment
There was a problem hiding this comment.
one minor query in test
bewithgaurav
previously approved these changes
Aug 6, 2025
… jahnvi/execute_cursor
gargsaumya
previously approved these changes
Aug 25, 2025
sumitmsft
previously approved these changes
Aug 26, 2025
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#34896](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34896) > [AB#34895](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34895) ------------------------------------------------------------------- ### Summary This pull request enhances the `Cursor` class in `mssql_python` to support Python's iterator protocol, adds a `next()` method for compatibility, and introduces comprehensive test cases to validate these features. The changes improve usability by enabling direct iteration over query results and ensure backward compatibility with existing code. ### Enhancements to `Cursor` functionality: * Added `__iter__` and `__next__` methods to the `Cursor` class, allowing it to be used as an iterator in for-loops and with the `next()` function. The `next()` method is also included as an alias for `__next__` to maintain compatibility with older code. (`mssql_python/cursor.py`, [[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R44) [[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R540-R581) ### New test cases for iteration and `next()` functionality: * Introduced `test_chaining_with_iteration` to validate iteration over query results using for-loops. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1529-R1774](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-R1774)) * Added `test_cursor_next_functionality` to confirm correct behavior of the `next()` method, including handling of empty result sets and single-row queries. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1529-R1774](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-R1774)) * Created `test_cursor_next_with_different_data_types` to ensure `next()` handles various data types correctly. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1529-R1774](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-R1774)) * Added `test_cursor_next_error_conditions` to test edge cases, such as calling `next()` on a closed cursor or before executing a query. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1529-R1774](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-R1774)) * Developed `test_future_iterator_protocol_compatibility` to demonstrate future compatibility with Python's iterator protocol. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1529-R1774](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1529-R1774)) ### Real-world usage examples: * Added `test_execute_chaining_compatibility_examples` to showcase practical use cases of iteration and chaining, such as fetching rows, updating, and deleting records. (`tests/test_004_cursor.py`, [tests/test_004_cursor.pyR1826-R1885](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R1826-R1885)) --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
6223304
sumitmsft
previously approved these changes
Aug 28, 2025
gargsaumya
previously approved these changes
Aug 28, 2025
bewithgaurav
previously approved these changes
Aug 28, 2025
3c47985
bewithgaurav
approved these changes
Aug 28, 2025
sumitmsft
approved these changes
Aug 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request enhances the
Cursorclass inmssql_python/cursor.pyto support method chaining by modifying theexecutemethod to return the cursor itself. It also introduces comprehensive test coverage for method chaining intests/test_004_cursor.py.Enhancements to the
Cursorclass:executemethod inmssql_python/cursor.pyto return theCursorinstance, enabling method chaining. (`[[1]]Updates to test cases:
test_longwvarchartest by removing unnecessary loops and assertions, improving readability.executewithfetchone,fetchall,fetchmany,rowcount, anddescription.